package www;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class Controller extends HttpServlet {
	@SuppressWarnings("rawtypes")
    private Map map = new HashMap();

    @SuppressWarnings("unchecked")
	public void init(ServletConfig config) throws ServletException {
        // configFile = "/WEB-INF/config/action-config.properties";
        String configFile = config.getInitParameter("configFile");
        // System.out.println("configFile: " + configFile);

        ServletContext ctx = config.getServletContext();
        configFile = ctx.getRealPath(configFile);

        Properties prop = new Properties();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(configFile);
            prop.load(fis); //
        } catch (IOException e) {
            throw new ServletException(e);
        } finally {
            if (fis != null)
                try {
                    fis.close();
                } catch (IOException ex) {
                }
        }

        @SuppressWarnings("rawtypes")
        Set keySet = prop.keySet();

        @SuppressWarnings("rawtypes")
        Iterator keyIter = keySet.iterator();

        System.out.println("---------------------------------------------------");
        System.out.println("properties reading.");
        System.out.println("---------------------------------------------------");

        while (keyIter.hasNext() == true) {

            // /mvcpds/createForm.do.....
            String key = (String) keyIter.next();

            // www.mvc.pds.CreateFormAction.....
            String val = prop.getProperty(key);
            System.out.println("Loading..... " + val);

            try {
                @SuppressWarnings("rawtypes")
                Class cls = Class.forName(val);
                Object inst = cls.newInstance();

                map.put(key, inst);
            } catch (Exception e) {
                throw new ServletException(e);
            }
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        process(request, response);

    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        process(request, response);

    }

    private void process(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        ActionForward forward = null;

        String uri = request.getRequestURI();
        if (uri.indexOf(request.getContextPath()) == 0) {
            int length = request.getContextPath().length();
            uri = uri.substring(length);
        }

        Action action = (Action) map.get(uri);

        if (action == null) {
            action = new NullAction();
        }

        try {
            forward = action.execute(request, response);
        } catch (Throwable e) {
            throw new ServletException(e);
        }

        if (forward.getRedirect() == true) { // redirect == true
            response.sendRedirect(request.getContextPath() + forward.getPath());
        } else { // redirect == false

            RequestDispatcher dispatcher = request.getRequestDispatcher(forward
                    .getPath());
            dispatcher.forward(request, response);
        }
    }
}
